<template>
  <el-dialog :title="title" :visible.sync="isShow" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false" @open="openEvent">
    <el-form :model="form">
      <el-row>
        <el-col :span="12">
          <el-form-item label="公司名称" :label-width="formLabelWidth">
            <el-input v-model="company.companyName" autocomplete="off" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上级部门" :label-width="formLabelWidth">
            <el-cascader v-model="form.pId" :options="orgOptions" :props="{ checkStrictly: true,emitPath:false }" @change="handleChange"></el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门名称" :label-width="formLabelWidth">
            <el-input v-model="form.orgName" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="状态" label-width="80px">
            <el-radio-group v-model="form.status">
              <el-radio label="0">正常</el-radio>
              <el-radio label="1">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="排序" label-width="80px">
            <el-input v-model="form.sort" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="operateAppInfo">确 定</el-button>
    </div>
  </el-dialog>
</template>
      
<script>
import { getOrgList, addOrg, updateOrg } from '@/api/organization'

export default {
  name: 'OrgInfoComp',
  data () {
    return {
      form: {
        companyId: '',
        pId: '0',
        orgName: '',
        sort: 0,
        status: "0"
      },
      formLabelWidth: '120px',
      title: '新增部门',
      orgOptions: []
    }
  },
  props: {
    isShow: {
      type: Boolean,
      required: true
    },
    company: {
      type: Object,
      require: true
    },
    item: {
      type: Object,
      require: false
    }
  },
  methods: {
    openEvent () {
      if (this.item === undefined) {
        this.form.companyId = this.company === undefined ? "" : this.company.id
        this.form.pId = "0"
        this.form.orgName = ""
        this.form.status = "0"
        this.form.sort = 0
        this.title = '新增部门'
      } else {
        this.title = '编辑部门'
        this.form = { ...this.item }
      }
      this.getOrgList()
    },
    getOrgList () {
      const param = { companyId: this.company.id }
      getOrgList(param).then((res) => {
        if (res.status === 200) {
          const data = res.data
          this.orgOptions = this.getCascaderOptions(data, "0")
        } else {
          this.$message(res.message);
        }
      });
    },
    getCascaderOptions (data, parentId) {
      const options = []
      data.forEach(element => {
        if (element.pId === parentId) {
          let item = { value: element.id, label: element.orgName }
          const children = this.getCascaderOptions(data, element.id)
          if (children.length > 0) {
            item.children = children
          }
          options.push(item)
        }
      });
      return options
    },
    handleChange (value) {
      this.form.pId = value
      console.log(value);
    },
    closeDialog () {
      this.$emit("changeDialogStatus", false);
    },
    result (data) {
      this.$emit("changeDialogStatus", "success");
      if (data.status === 200) {
        this.$message.success("操作成功");
      } else {
        this.$message.error("操作失败，请重试");
      }
    },
    operateAppInfo () {
      const param = { ...this.form }
      if (this.item === undefined) {
        addOrg(param).then((res) => {
          this.result(res)
        });
      } else {
        updateOrg(param).then((res) => {
          this.result(res)
        });
      }
    }
  },
  watch: {
    // 监听company数据的变化
    company (newValue, oldValue) {
      console.log(`company changed from ${oldValue} to ${newValue}`);
      // 在这里可以执行更多的逻辑
      if (newValue !== null || newValue !== undefined) {
        this.getOrgList()
      }
    }
  }
}
</script>
      
<style scoped>
</style>
      